package com.gimi.cloud.bbp.manage.service.mall.impl;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.ReflectUtils;
import com.gimi.cloud.bbp.dao.dao.mall.MallAppRecommentDao;
import com.gimi.cloud.bbp.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.bbp.dto.mall.MallAppRecommentDTO;
import com.gimi.cloud.bbp.entity.mall.MallAppRecommentEntity;
import com.gimi.cloud.bbp.manage.service.mall.MallAppRecommentService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 推荐管理相关接口实现类
 * @author chendi
 * @date 2018年2月1日
 */
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class MallAppRecommentServiceImpl implements MallAppRecommentService {
    private static final Logger logger = LoggerFactory.getLogger(MallAppRecommentServiceImpl.class);
    private MallAppRecommentDao mallAppRecommentDao;

    @Autowired
    public MallAppRecommentServiceImpl(MallAppRecommentDao mallAppRecommentDao) {
        this.mallAppRecommentDao = mallAppRecommentDao;
    }

    @Override
    public List<MallAppRecommentDTO> queryList(MallAppRecommentDTO dto, boolean isPaging) throws Exception {
        if (isPaging){
            PageHelper.startPage(dto.getPageNums(),dto.getPageSizes());
        }
        String sql = "SELECT rec.* FROM mall_app_recomment rec WHERE rec.is_delete = 0 AND rec.arg_id = :argId AND rec.abt_status = :abtStatus ORDER BY rec.abt_sort";
        QueryParam param = new QueryParam();
        param.setSql(sql);
        param.setParamObject(dto);
        return mallAppRecommentDao.selectListDTOBySql(param);
    }

    @Override
    public MallAppRecommentDTO load(MallAppRecommentDTO dto, Object id) throws Exception {
        MallAppRecommentEntity recommentEntity = mallAppRecommentDao.queryBy("abt_id",id);
        if(null == recommentEntity){
            return null;
        }
        MallAppRecommentDTO DTO = new MallAppRecommentDTO();
        ReflectUtils.copyPropertiesIgnoreNull(recommentEntity,DTO);
        return DTO;
    }

    @Override
    public MallAppRecommentDTO saveOrUpdate(MallAppRecommentDTO dto) throws Exception {
        MallAppRecommentEntity recommentEntity = null;
        if (null != dto.getAbtId()) {
            recommentEntity = mallAppRecommentDao.queryBy("abt_id", dto.getAbtId());
            if (null == recommentEntity) {
                throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取recommectid为" + dto.getAbtId() + "的推送信息，更新失败");
            }
        } else {
            recommentEntity = new MallAppRecommentEntity();
        }
        ReflectUtils.copyPropertiesIgnoreNull(dto, recommentEntity);
        if (null != dto.getAbtId()) {
            mallAppRecommentDao.baseUpdateByPrimaryKey(recommentEntity);
        } else {
            mallAppRecommentDao.baseInsert(recommentEntity);
        }
        ReflectUtils.copyPropertiesIgnoreNull(recommentEntity, dto);
        return dto;
    }

    @Override
    public int delete(MallAppRecommentDTO dto, Object id, boolean realDel) throws Exception {
        MallAppRecommentEntity recommentEntity = mallAppRecommentDao.queryBy("abt_id", id);
        if (null == recommentEntity) {
            throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取recommentid为" + id + "的推送信息，删除失败");
        }
        //非物理删除
        if(!realDel){
            ReflectUtils.copyPropertiesIgnoreNull(dto,recommentEntity);
            recommentEntity.setIsDelete(1);
            return mallAppRecommentDao.baseUpdateByPrimaryKey(recommentEntity);
        }
        return mallAppRecommentDao.deleteByPrimaryKey(id);
    }

    @Override
    public Integer deleteRecommentByIds(MallAppRecommentDTO dto, List<Integer> recommentIds, Integer argId) throws Exception {
        String recommentId = "";
        for (Integer str : recommentIds) {
            recommentId += str + ",";
        }
        String sql = "UPDATE mall_app_recomment SET is_delete = 1,update_uid = '" + dto.getUid()
                + "',update_uname='" + dto.getUname() + "',update_time =NOW() " +
                " WHERE arg_id =" + dto.getArgId();
        if(recommentId.length() > 0){
            recommentId = recommentId.substring(0, recommentId.length() - 1);
            sql = sql + " AND abt_id not in(" + recommentId + ")";
        }
        QueryParam queryParam = new QueryParam();
        queryParam.setSql(sql);
        return mallAppRecommentDao.updateSql(queryParam);
    }
}
